package org.shock.ss.sms.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;

import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.bean.Tree;
import org.shock.ss.sms.bean.system.Dept;
import org.shock.ss.sms.bean.system.Dic;
import org.shock.ss.sms.bean.system.SysPortal;
import org.shock.ss.sms.bean.system.User;
import org.shock.ss.sms.bean.system.UserInfor;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.impl.UserDaoImpl;
import org.shock.ss.sms.service.system.UserService;
import org.shock.ss.sms.service.system.impl.UserServiceImpl;

import org.shock.ss.sms.action.BaseAction;

/**
 * @author Administrator
 * 
 */
public class ListAction extends BaseAction {

	/**
	 * 生成常用list
	 */
	private static final long serialVersionUID = 1L;
	private List userList = new ArrayList();
	private Integer bmid = 1;
	private Long roleid;
	private String ids = "0";
	private int cascade = 1; // 使用oracle connect by级联 0不使用
	private String rybh;
	private String ryxm;
	private List list = new ArrayList();
	private String regionid;
	private UserService us = new UserServiceImpl((UserDaoImpl) DaoFactory.getDao("UserDao"));

	public Integer getBmid() {
		return bmid;
	}

	public void setBmid(Integer bmid) {
		this.bmid = bmid;
	}

	public Long getRoleid() {
		return roleid;
	}

	public void setRoleid(Long roleid) {
		this.roleid = roleid;
	}

	public String userList() throws Exception {
		if (!"".equals(ids)) {
			sqladd.append(" and id not in(" + ids + ")");
		}
		String sql = "select ygxm||'('||ygzw||')' ygxm,id from " + User.getTable() + " where 1=1 order by paixu";
		if (null != bmid) {
			if (cascade == 1) {
				sql = "select ygxm||'('||ygzw||')' ygxm,id from " + User.getTable() + " where 1=1 " + sqladd
						+ " and bmid in (select id from dept start with id in(" + bmid
						+ ") connect by prior id = sjbmid)";
			} else {
				sql = "select ygxm||'('||ygzw||')' ygxm,id from " + User.getTable() + " where 1=1 " + sqladd
						+ " and bmid =" + bmid;
			}

		}
		if (roleid != null) {
			if (cascade == 1) {
				List bmidList = new ArrayList<Long>();
				String bmids = DbTools.getSubIdsStr(bmidList, Dept.getTable(), "sjbmid", bmid + "") + "," + bmid;
				sql = "select  concat(ygxm,'(',ygzw,')') ygxm,id from " + User.getTable()
						+ " where ','||roleids||',' like '%," + roleid + ",%' and bmid in (" + bmids + ")";
				System.out.println(sql);
			} else {
				sql = "select  concat(ygxm,'(',ygzw,')') ygxm,id from " + User.getTable()
						+ " where ','||roleids||',' like '%," + roleid + ",%' and bmid = " + bmid;
			}

		}

		userList = DbTools.getList(sql);
		jsonString = "{list:" + JSON.toJSONString(userList) + "}";
		return SUCCESS;
	}

	public String user4select() throws Exception {
		String sql = "select ygxm,ygzh from " + User.getTable() + " where 1=1 order by paixu";

		if (!"".equals(ids)) {
			sql = "select ygxm,ygzh from " + User.getTable() + " where 1=1 " + sqladd + " and bmid in (" + ids + ") ";
		}
		if (roleid != null) {
			sql = "select ygxm,ygzh from " + User.getTable() + " where ','||roleids||',' like '%," + roleid + ",%'";
		}
		userList = DbTools.getList(sql);
		if (userList.size() > 500) {// 如果用户数太大则只取前500名
			userList = userList.subList(0, 500);
		}
		jsonString = "{list:" + JSON.toJSONString(userList) + "}";
		return SUCCESS;
	}

	/**
	 * 在线用户列表
	 */
	public String onlineAppUser() throws Exception {
		List list = (List) ActionContext.getContext().getApplication().get("online");
		for (int i = 0; i < list.size(); i++) {
			Map map = new HashMap();
			map.put("value", ((UserInfor) list.get(i)).getYgzh());
			map.put("text", ((UserInfor) list.get(i)).getYgxm());
			userList.add(map);
		}
		jsonString = "{list:" + JSON.toJSONString(userList) + "}";
		return SUCCESS;
	}

	/** 用于产生区域选择树，供Ext选择 */
	public String genSysAreaTree() throws Exception {
		String sql = "select value,code from " + Dic.getTable() + " where pcode='AREA' order by paixu";
		list = DbTools.getMapsInList(sql);
		List tmpList = new ArrayList();
		for (Map map : (ArrayList<Map>) list) {
			Tree tree = new Tree();
			tree.setId((String) map.get("code"));
			tree.setText((String) map.get("value"));
			tree.setLeaf(true);
			tree.setChecked(false);
			tmpList.add(tree);
		}
		jsonString = JSONArray.fromObject(tmpList).toString();
		// System.out.println(jsonString);
		return SUCCESS;
	}
	public String listArea() {
		String sql = "select areaid,area from area where fatherid=" + regionid;
		System.out.println(sql);
		list = DbTools.getMapsInList(sql);
		jsonString = JSON.toJSONString(list);
		return SUCCESS;
	}
	public String listCity() {
		String sql = "select cityid,city from city where fatherid=" + regionid;
		System.out.println(sql);
		list = DbTools.getMapsInList(sql);
		jsonString = JSON.toJSONString(list);
		return SUCCESS;
	}

	public String listProvince() {
		String sql = "select provinceid,province from province";
		System.out.println(sql);
		list = DbTools.getMapsInList(sql);
		jsonString = JSON.toJSONString(list);
		return SUCCESS;
	}

	public List getUserList() {
		return userList;
	}

	public void setUserList(List userList) {
		this.userList = userList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	/**
	 * @return the cascade
	 */
	public int getCascade() {
		return cascade;
	}

	/**
	 * @param cascade
	 *            the cascade to set
	 */
	public void setCascade(int cascade) {
		this.cascade = cascade;
	}

	/**
	 * @return the rybh
	 */
	public String getRybh() {
		return rybh;
	}

	/**
	 * @param rybh
	 *            the rybh to set
	 */
	public void setRybh(String rybh) {
		this.rybh = rybh;
	}

	/**
	 * @return the ryxm
	 */
	public String getRyxm() {
		return ryxm;
	}

	/**
	 * @param ryxm
	 *            the ryxm to set
	 */
	public void setRyxm(String ryxm) {
		this.ryxm = ryxm;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}


	public String getRegionid() {
		return regionid;
	}

	public void setRegionid(String regionid) {
		this.regionid = regionid;
	}

}
